// pages/game/game.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    seconds: 60,
    score: 0,
    timer: null,
    targetvalue: "红",
    targetisred: true,
    optionlvalue: "红",
    optionlisred: true,
    optionrvalue: "蓝",
    optionrisred: false,
    question: [{targetvalue: "红",  targetisred: true,  optionlvalue: "红",  optionlisred: true,  optionrvalue: "蓝",  optionrisred: true,}, 
    {targetvalue: "红",  targetisred: true,  optionlvalue: "红",  optionlisred: true,  optionrvalue: "蓝",  optionrisred: false,}, 
    {targetvalue: "红",  targetisred: true,  optionlvalue: "红",  optionlisred: false,  optionrvalue: "蓝",  optionrisred: true,}, 
    {targetvalue: "红",  targetisred: true,  optionlvalue: "红",  optionlisred: false,  optionrvalue: "蓝",  optionrisred: false,}, 
    {targetvalue: "红",  targetisred: false,  optionlvalue: "红",  optionlisred: true,  optionrvalue: "蓝",  optionrisred: true,}, 
    {targetvalue: "红",  targetisred: false,  optionlvalue: "红",  optionlisred: true,  optionrvalue: "蓝",  optionrisred: false,}, 
    {targetvalue: "红",  targetisred: false,  optionlvalue: "红",  optionlisred: false,  optionrvalue: "蓝",  optionrisred: true,}, 
    {targetvalue: "红",  targetisred: false,  optionlvalue: "红",  optionlisred: false,  optionrvalue: "蓝",  optionrisred: false,}, 
    {targetvalue: "蓝",  targetisred: true,  optionlvalue: "红",  optionlisred: true,  optionrvalue: "蓝",  optionrisred: true,}, 
    {targetvalue: "蓝",  targetisred: true,  optionlvalue: "红",  optionlisred: true,  optionrvalue: "蓝",  optionrisred: false,}, 
    {targetvalue: "蓝",  targetisred: true,  optionlvalue: "红",  optionlisred: false,  optionrvalue: "蓝",  optionrisred: true,}, 
    {targetvalue: "蓝",  targetisred: true,  optionlvalue: "红",  optionlisred: false,  optionrvalue: "蓝",  optionrisred: false,}, 
    {targetvalue: "蓝",  targetisred: false,  optionlvalue: "红",  optionlisred: true,  optionrvalue: "蓝",  optionrisred: true,}, 
    {targetvalue: "蓝",  targetisred: false,  optionlvalue: "红",  optionlisred: true,  optionrvalue: "蓝",  optionrisred: false,}, 
    {targetvalue: "蓝",  targetisred: false,  optionlvalue: "红",  optionlisred: false,  optionrvalue: "蓝",  optionrisred: true,}, 
    {targetvalue: "蓝",  targetisred: false,  optionlvalue: "红",  optionlisred: false,  optionrvalue: "蓝",  optionrisred: false,}, 
    {targetvalue: "红",  targetisred: true,  optionlvalue: "蓝",  optionlisred: true,  optionrvalue: "红",  optionrisred: true,}, 
    {targetvalue: "红",  targetisred: true,  optionlvalue: "蓝",  optionlisred: true,  optionrvalue: "红",  optionrisred: false,}, 
    {targetvalue: "红",  targetisred: true,  optionlvalue: "蓝",  optionlisred: false,  optionrvalue: "红",  optionrisred: true,}, 
    {targetvalue: "红",  targetisred: true,  optionlvalue: "蓝",  optionlisred: false,  optionrvalue: "红",  optionrisred: false,}, 
    {targetvalue: "红",  targetisred: false,  optionlvalue: "蓝",  optionlisred: true,  optionrvalue: "红",  optionrisred: true,}, 
    {targetvalue: "红",  targetisred: false,  optionlvalue: "蓝",  optionlisred: true,  optionrvalue: "红",  optionrisred: false,}, 
    {targetvalue: "红",  targetisred: false,  optionlvalue: "蓝",  optionlisred: false,  optionrvalue: "红",  optionrisred: true,}, 
    {targetvalue: "红",  targetisred: false,  optionlvalue: "蓝",  optionlisred: false,  optionrvalue: "红",  optionrisred: false,}, 
    {targetvalue: "蓝",  targetisred: true,  optionlvalue: "蓝",  optionlisred: true,  optionrvalue: "红",  optionrisred: true,}, 
    {targetvalue: "蓝",  targetisred: true,  optionlvalue: "蓝",  optionlisred: true,  optionrvalue: "红",  optionrisred: false,}, 
    {targetvalue: "蓝",  targetisred: true,  optionlvalue: "蓝",  optionlisred: false,  optionrvalue: "红",  optionrisred: true,}, 
    {targetvalue: "蓝",  targetisred: true,  optionlvalue: "蓝",  optionlisred: false,  optionrvalue: "红",  optionrisred: false,}, 
    {targetvalue: "蓝",  targetisred: false,  optionlvalue: "蓝",  optionlisred: true,  optionrvalue: "红",  optionrisred: true,}, 
    {targetvalue: "蓝",  targetisred: false,  optionlvalue: "蓝",  optionlisred: true,  optionrvalue: "红",  optionrisred: false,}, 
    {targetvalue: "蓝",  targetisred: false,  optionlvalue: "蓝",  optionlisred: false,  optionrvalue: "红",  optionrisred: true,}, 
    {targetvalue: "蓝",  targetisred: false,  optionlvalue: "蓝",  optionlisred: false,  optionrvalue: "红",  optionrisred: false,}, ]
  },

  timer() {
    const that = this
    that.data.timer = setInterval(function () {
      if (that.data.seconds < 1) {
        // console.log("time out")
        clearInterval(that.data.timer)
        wx.redirectTo({
          url: '/pages/result/result?score=' + that.data.score,
        })
      } else {
        that.data.seconds--
        that.setData({
          seconds: that.data.seconds
        })
      }
    }, 1000)
  },

  addscore() {
    const that = this
    that.data.score++
    that.setData({
      score: that.data.score
    })
  },

  showquestion(){
    const index = Math.floor(Math.random()*32)
    const question = this.data.question[index]
    this.setData({
      targetvalue: question.targetvalue,
      targetisred: question.targetisred,
      optionlvalue: question.optionlvalue,
      optionlisred: question.optionlisred,
      optionrvalue: question.optionrvalue,
      optionrisred: question.optionrisred,
    })
  },

  myToast(msg, icon, time = 600){
    wx.showToast({
      title: msg,
      icon: icon,
      duration: time
    })
  },

  checkoption(event){
    const that = this
    if(this.data.seconds === 0)
      return
    if(event.currentTarget.dataset.pos === 'L'){
      if((that.data.targetisred && (that.data.optionlvalue === '红'))||(!that.data.targetisred && (that.data.optionlvalue === '蓝'))){
        this.myToast('回答正确', 'success')
        that.addscore()
      } else {
        this.myToast('回答错误', 'error')
      }
      
    }
    if(event.currentTarget.dataset.pos === 'R'){
      if((that.data.targetisred && (that.data.optionrvalue === '红'))||(!that.data.targetisred && (that.data.optionrvalue === '蓝'))){
        this.myToast('回答正确', 'success')
        that.addscore()
      } else {
        this.myToast('回答错误', 'error')
      }
      
    }

    that.showquestion()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.timer()
    this.showquestion()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // console.log("onReady")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // console.log("onShow")
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    // console.log("onHide")
    const that = this
    clearInterval(that.data.timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // console.log("onUnload")
    const that = this
    clearInterval(that.data.timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})